require(['grid'], function () {
    var $grid = $("#gridList");
    $grid.grid({
        url: "/electFencSet/searchElectFencCarRelByCarId.htm",
        multiselect: false,
        showPager: false,
        width: 450,
        height: 410,
        colModel: [
            {name: "fencId", key: true, label: "", hidden: true},
            {name: "fencName", label: "电子围栏名称", width: 280},
            {
                name: "driveDrc", label: "方向", align: "center", width: 150,
                formatter: function (value) {
                    var boolIn = '', boolOut = '';
                    if (value === 'FENC0001') {
                        boolIn = 'checked';
                    } else if (value === 'FENC0002') {
                        boolOut = 'checked';
                    } else if (value == 'FENC0003') {
                        boolIn = 'checked';
                        boolOut = 'checked';
                    }
                    return ('<label class="radio-wrap">' +
                    '           <input type="checkbox" name="shiru" {0}>' +
                    '           <span class="span-text">驶入</span>' +
                    '       </label>' +
                    '       <label class="radio-wrap">' +
                    '           <input type="checkbox" name="shichu" style="margin-left: 10px;" {1}>' +
                    '           <span class="span-text">驶出</span>' +
                    '       </label>').format(boolIn, boolOut);
                },
                unformat: function (value) {
                    return value;
                }
            }
        ]
    });

    var currentIds = [];//当前的要操作的车辆ID数组
    var $filterText = $('#filterText'),//过滤条件输入框
        $searchBtn = $('#searchBtn');//查询按钮

    $searchBtn.on('click', function () {

//$($($t).jqGrid('getGridRowById', id)).css("display","none");
        var data = $('#gridList').grid('getRowData');
        var param = {};
        var trRow;

        var filterText = $filterText.val().trim();
        if (filterText) {
            $.each(data, function (i, item) {
                trRow = $('#gridList').grid('getGridRowById', item.fencId);
                $(trRow).css('display', item.fencName.indexOf(filterText) > -1 ? '' : 'none');

            });
        } else {
            $('tr:gt(0)', $grid).css('display', '');
        }

    });

    /**
     * 根据车辆ID查询该车辆绑定的电子围栏信息及所有的电子围栏
     * carId为空就查询所有的电子围栏
     * @param carId
     */
    window.loadData = function (carId) {
        if ($.isArray(carId)) {
            currentIds = carId;
        } else {
            currentIds = [carId];
        }
        $grid.grid('setGridParam', {
            datatype: "json",
            postData: {
                carId: $.isArray(carId) ? '' : (carId || '')
            }
        }).trigger("reloadGrid");
    };

    /**
     * 获取有勾选驶入或者驶出的电子围栏信息
     * @returns {{}}
     */
    function getSelectElectFence() {
        var data = $('#gridList').grid('getRowData');
        var param = {};
        var trRow;
        var boolIn, boolOut;//是否驶入、是否驶出
        var index = 0;
        var driveDrc;//驶入驶出状态
        $.each(data, function (i, item) {
            trRow = $('#gridList').grid('getGridRowById', item.fencId);
            boolIn = $(':checkbox[name=shiru]', trRow).prop('checked');
            boolOut = $(':checkbox[name=shichu]', trRow).prop('checked');
            if (boolIn || boolOut) {

                if (boolIn && boolOut) {
                    driveDrc = 'FENC0003';
                } else if (boolIn) {
                    driveDrc = 'FENC0001';
                } else if (boolOut) {
                    driveDrc = 'FENC0002';
                }

                param["electFencList[" + index + "].fencId"] = item.fencId;
                param["electFencList[" + index + "].driveDrc"] = driveDrc;
                index++;
            }
        });
        if (index > 5) {
            $.messager.alert('提示', '最多只能选择6个电子围栏！');
            return null;
        }
        return param;
    }

    /**
     * 下发绑定电子围栏
     * @param callback
     */
    window.issue = function (callback) {
        $('tr:gt(0)', $grid).css('display', '');
        var param = getSelectElectFence();
        if (!$.isEmptyObject(param) && currentIds.length) {
            param.orgIds = currentIds;
            $.ajaxExt({
                url: '/electFencSet/setElectFenc.htm',
                data: param
            }).done(callback || $.noop)
        }
    };

    window.clear = function () {
        currentIds = [];
        $filterText.val('');
        $grid.grid('clearGridData');
    };
});